/*
 * @Author: 肖燕 2461435122@qq.com
 * @Date: 2022-09-23 17:20:12
 * @LastEditors: 肖燕 2461435122@qq.com
 * @LastEditTime: 2023-08-03 14:23:35
 * @FilePath: /mris-platform-all/src/assets/scripts/tools/route.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from '@/router/index.js'
// 配置
import { searchMainPageDetailByNo, queryDrgSettle } from 'mris-component-package/lib/services/apis/drg.js'
import { $message } from '@/assets/scripts/tools/tips.js'
import store from '@/assets/scripts/store'
import storage from '@/assets/scripts/store/user'
import { ACCESS_DEFWS } from '@/store/mutation-types'
import _this from '../../../main'
import { isWsDoctor } from '@/router/getws-router'

export function handleRouteByPath (path, params, name) {
  if (window.ElectronRouter) {
    // const navData = getNavByLink(path)
    // window.ElectronRouter({
    //   url: window.location.origin + '#' + path,
    //   path: path,
    //   name: navData.name,
    //   from: window.location.href,
    //   isReload
    // })
  } else {
    if (name) {
      router.push({ name: name, params: params })
    } else {
      // 参数带在链接上
      router.push({ path: path, query: params })
    }
  }
}

// 根据ID获取病案信息 再跳转页面_新开浏览器窗口_无搜索列表
export async function handleCaseRecord (data = {}, ids = [], isMock = true) {
  const medRecNo = data.medRecNo || data.patientId
  data.mainpage_type = data.mainpage_type || 2
  if (medRecNo) {
    const res = data.isSettlement ? await queryDrgSettle({ medRecNo }) : await searchMainPageDetailByNo(medRecNo)
    if (res.data) {
      store.commit('SET_MAINPAGE', Object.assign({}, data, res.data))
      const wscode = storage.get(ACCESS_DEFWS)
      const { href } = router.resolve({
        name: 'CaseRecord' + wscode.replace(wscode[0], wscode[0].toUpperCase()),
        query: {}
      })
      localStorage.setItem('caseInfo', JSON.stringify(data.caseInfo))
      localStorage.setItem('ids', JSON.stringify(ids))
      window.open(href, '_blank')
    } else {
      $message({ type: 'warning', message: '未查询到病案信息！' })
    }
  }
}

// 异步导出跳转导出日志，下载对应文件
export function handleRouteEplog (link, cb, title = '导出日志') {
  const h = _this.$createElement
  _this.$msgbox({
    title: '提示',
    message: h('p', null, [
      h('span', null, '操作成功，即将导出'),
      h('span', { class: 'ud-bold ud-success' }, '「请前往【' + title + '】，下载对应文件」'),
      h('span', null, '立即前往？')
    ]),
    showCancelButton: true,
    confirmButtonText: '确 认',
    cancelButtonText: '取 消',
    type: 'success'
  }).then(() => {
    if (cb) {
      cb()
      return false
    }
    let defaultUrl = 'mris-cash/pages/ptlog/index'
    if (isWsDoctor()) {
      defaultUrl = 'mris-doctor/pages/ptlog/index'
    }
    const wscode = storage.get(ACCESS_DEFWS)
    let resUrl = link || defaultUrl
    if (wscode === 'settlement') {
      resUrl = 'mris-settlement/pages/ptlog/index'
    }
    handleRouteByPath('', '', resUrl)
  }).catch(() => {})
}
